home *** CD-ROM | disk | FTP | other *** search
- //____________________________________________________________
- // ChangeVoice.c
- //
- // Copyright © Dan Parks Sydow, 1995
- // From the book:
- // "Graphics and Sound Programming Techniques for the Mac",
- // M&T Books, 1995
-
-
- //____________________________________________________________
-
- #include <Speech.h>
-
-
- //____________________________________________________________
-
- void InitializeToolbox( void );
- Boolean IsSpeechAvailable( void );
- SpeechChannel OpenOneSpeechChannelVoice( VoiceSpec );
- void OpenSpeechDialog( void );
- OSErr GetVoiceSpecBasedOnAgeGender( VoiceSpec *, short, short, short );
-
-
- //____________________________________________________________
-
- #define rSpeechDialog 128
- #define kPlaySpeechButton 1
- #define kSetSpeechYoungBoyButton 2
- #define kSetSpeechMiddleAgeWomanButton 3
- #define kSetSpeechRobotButton 4
- #define kSetSpeechDefaultButton 5
- #define kQuitButton 6
- #define kNoMatchingVoiceErr -999
-
- //____________________________________________________________
-
- void main( void )
- {
- Boolean speechPresent;
-
- InitializeToolbox();
-
- speechPresent = IsSpeechAvailable();
- if ( speechPresent == false )
- ExitToShell();
-
- OpenSpeechDialog();
- }
-
-
- //____________________________________________________________
-
- void OpenSpeechDialog( void )
- {
- DialogPtr theDialog;
- short theItem;
- Boolean allDone = false;
- OSErr theError;
- SpeechChannel theChannel;
- Str255 theString = "\pMilwaukee, that's in Minnesota isn't it?";
- short theAgeLo;
- short theAgeHi;
- short theGender;
- VoiceSpec theDefaultVoiceSpec;
- VoiceSpec theVoiceSpec;
- VoiceDescription theVoiceDesc;
-
- theDialog = GetNewDialog( rSpeechDialog, nil, (WindowPtr)-1L );
- ShowWindow( theDialog );
- SetPort( theDialog );
-
- theError = GetVoiceDescription( nil, &theVoiceDesc, sizeof( theVoiceDesc ) );
-
- theDefaultVoiceSpec = theVoiceDesc.voice;
- theVoiceSpec = theDefaultVoiceSpec;
-
- while ( allDone == false )
- {
- ModalDialog( nil, &theItem );
-
- switch ( theItem )
- {
- case kSetSpeechYoungBoyButton:
- theGender = kMale;
- theAgeLo = 5;
- theAgeHi = 10;
- theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo,
- theAgeHi, theGender );
- break;
-
- case kSetSpeechMiddleAgeWomanButton:
- theGender = kFemale;
- theAgeLo = 30;
- theAgeHi = 50;
- theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo,
- theAgeHi, theGender );
- break;
-
- case kSetSpeechRobotButton:
- theGender = kNeuter;
- theAgeLo = 500;
- theAgeHi = 10000;
- theError = GetVoiceSpecBasedOnAgeGender( &theVoiceSpec, theAgeLo,
- theAgeHi, theGender );
- break;
-
- case kSetSpeechDefaultButton:
- theVoiceSpec = theDefaultVoiceSpec;
- break;
-
- case kPlaySpeechButton:
- theChannel = OpenOneSpeechChannelVoice( theVoiceSpec );
- if ( theChannel == nil )
- ExitToShell();
- theError = SpeakText( theChannel, (Ptr)(theString + 1), theString[0] );
- while ( SpeechBusy() == true )
- ;
- theError = DisposeSpeechChannel( theChannel );
- if ( theError != noErr )
- ExitToShell();
- break;
-
- case kQuitButton:
- allDone = true;
- break;
- }
-
- if ( theError == kNoMatchingVoiceErr )
- {
- SysBeep( 0 );
- theVoiceSpec = theDefaultVoiceSpec;
- }
- else if ( theError != noErr )
- ExitToShell();
- }
-
- DisposeDialog( theDialog );
- }
-
- //____________________________________________________________
-
- OSErr GetVoiceSpecBasedOnAgeGender( VoiceSpec *theVoiceSpec,
- short theAgeLo,
- short theAgeHi,
- short theGender )
- {
- OSErr theError;
- short theNumVoices;
- short theIndex;
- VoiceDescription theVoiceDesc;
-
- theError = CountVoices( &theNumVoices );
- if ( theError != noErr )
- return ( theError );
-
- for ( theIndex = 1; theIndex <= theNumVoices; theIndex++ )
- {
- theError = GetIndVoice( theIndex, theVoiceSpec );
- if ( theError != noErr )
- return ( theError );
-
- theError = GetVoiceDescription( theVoiceSpec, &theVoiceDesc,
- sizeof( theVoiceDesc ) );
- if ( theError != noErr )
- return ( theError );
-
- if ( (theVoiceDesc.age >= theAgeLo) && (theVoiceDesc.age <= theAgeHi) )
- if ( theVoiceDesc.gender == theGender )
- return ( noErr );
- }
- return ( kNoMatchingVoiceErr );
- }
-
-
- //____________________________________________________________
-
- SpeechChannel OpenOneSpeechChannelVoice( VoiceSpec theVoiceSpec )
- {
- SpeechChannel theChannel;
- OSErr theError;
-
- theError = NewSpeechChannel( &theVoiceSpec, &theChannel );
- if ( theError != noErr )
- {
- theError = DisposeSpeechChannel( theChannel );
- theChannel = nil;
- }
-
- return ( theChannel );
- }
-
-
- //____________________________________________________________
-
- Boolean IsSpeechAvailable( void )
- {
- OSErr theError;
- long theResult;
- Boolean speechAvail;
-
- theError = Gestalt( gestaltSpeechAttr, &theResult );
- if ( theError != noErr )
- ExitToShell();
-
- speechAvail = theResult & ( 1 << gestaltSpeechMgrPresent );
- if ( speechAvail > 0 )
- return ( true );
- else
- return ( false );
- }
-
-
- //____________________________________________________________
-
- void InitializeToolbox( void )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- FlushEvents( everyEvent, 0 );
- InitCursor();
- }
-
-
-
-